const app = getApp()
// pages/revise_address/address.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    user: {},
    text: '修改',
    content: '',
    duration: 2000,
    $zanui: {
      toptips: {
        show: false,
      },
    },
    addressId: '',
  },

  /**
   * 发送数据
   */
  sendData: function(e) {
    let arr = e.detail
    if (arr.name == '') {
      this._topCue('请输入姓名')
      return
    }
    if (arr.phone == '') {
      this._topCue('请输入电话号码')
      return
    }
    if (arr.region == '' || arr.region == '请选择') {
      this._topCue('请选择所在地区')
      return
    }
    if (arr.content.length <= 5) {
      this._topCue('详情地址不少于5个字')
      return
    }
    this.uploadAddress(arr)
  },

  /**
   * 修改收货地址
   */
  uploadAddress(e) {
    let _this = this
    let address = {
      id: _this.data.addressId,
      userId: app.globalDataO.openid.objectSid,
      address: e.region,
      detailed: e.content,
      isdefault: e.select,
      addressee: e.name,
      phone: e.phone,
      sex: 1,
    }
    app.post(app.url.address.editAddress, address).then(re => {
      this._topCue(re.data.msg)
      wx.navigateBack({
        delta: 1
      })
    })
  },

  /**
   * 顶部提示
   */
  _topCue: function(text) {
    this.setData({
      content: text,
      $zanui: {
        toptips: {
          show: true,
        },
      },
    })

    setTimeout(() => {
      this.setData({
        $zanui: {
          toptips: {
            show: false,
          },
        },
      })
    }, this.data.duration)
  },

  /**
   * 删除
   */
  _deleteData: function() {
  
    wx.showModal({
      title: '确定删除吗',
      success() {
  
        if (res.confirm) {
          let deleteData = {
            id: this.data.addressId,
          }
          app.postJson(app.url.address.deleteAddress, deleteData).then(re => {
            console.log('删除结果', re)
            if (re.statusCode == 200) {
              this._topCue(re.data.msg)
              wx.navigateBack({
                delta: 1
              })
            } else {
              this._topCue(re.data.msg)
            }
          })
        }else if(res.cancel){
          console.log('取消')
        }
      }
    })

  },
  // 获取当前收货地址
  getOneAddressPost: function() {
    let getdata = {
      addressId: this.data.addressId,
    }
    app.post(app.url.address.getOneAddress, getdata).then(re => {
      console.log('单个获取结果', re)
      if (re.statusCode == 200) {
        this.setData({
          user: {
            name: re.data.memAddressDO.addressee,
            phone: re.data.memAddressDO.phone,
            region: re.data.memAddressDO.address,
            content: re.data.memAddressDO.detailed,
            select: re.data.memAddressDO.isdefault
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log('获取参数', options)
    let user = options.address
    console.log(user)
    this.setData({
      addressId: user,
    })

    // 获取单个收货地址
    this.getOneAddressPost()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {},
})